﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace IslandInterloper.Engine
{
    internal class Islands:GameClientCollectionIntKeyed<Island>
    {
        internal Islands(Game theGame)
            : base(theGame)
        {
            //generate
            int id = 0;
            while (Keys.Count < Game.Config.IslandCount)
            {
                Island island;
                island = new Island(Game,id);
                //check name
                if (Keys.Contains(island.ID)) continue;
                //check location
                bool tooClose = false;
                foreach (int key in Keys)
                {
                    if (Location.Distance(island.Position, this[key].Position) < Game.Config.MinimumIslandDistance)
                    {
                        tooClose = true;
                        break;
                    }
                }
                if (!tooClose)
                {
                    collection.Add(island.ID, island);
                    id++;
                }
            }
        }
        internal Islands(Game theGame, XElement element)
            : base(theGame,element)
        {
        }
        protected override Island LoadItemFromXElement(XElement element)
        {
            return new Island(Game, element);
        }
        protected override XName KeyToXName(int theKey)
        {
            return GameXNames.island;
        }
    }
}
